<template>
    <div class="login-wrap">
        <!-- <div class="ms-title">矿机整机维修指导系统</div> -->
        <div class="ms-login">
            <div class="ms-title">矿机整机维修指导系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>

                <el-form-item prop="code">
                    <el-input
                        v-model="param.code"
                        auto-complete="off"
                        placeholder="验证码"
                        style="width: 63%"
                        @keyup.enter.native="submitForm"
                    >
                        <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
                    </el-input>

                    <div class="login-code">
                        <img :src="codeUrl" @click="getCode" class="login-code-img" />
                    </div>
                </el-form-item>

                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
var that;
import { login, getCodeImg } from '../../api/login';
import store from '../../store/index.js';
import { Toast } from 'vant';
export default {
    components: {
        [Toast.name]: Toast
    },
    data() {
        return {
            param: {
                username: 'admin',
                password: 'admin123',
                code:'',
                uuid:''
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            },
            codeUrl: ''
        };
    },
    created() {
        that = this;
        Toast.clear();
        this.getCode();
    },
    methods: {
        submitForm() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    // 加载提示框
                    Toast.loading({
                        duration: 0, //持续展示loading
                        forbidClick: true,
                        message: '登陆中...'
                    });
                    login({
                        username: this.param.username,
                        password: this.param.password,
                        code:this.param.code,
                        uuid:this.param.uuid
                    })
                        .then((res) => {
                            Toast.clear(); //关闭提示框
                            if (res.data.code === 401) {
                                that.$message.error('账号或密码错误');
                            } else if (res.data.code === 500){
                                that.$message.error(res.data.msg);
                                this.getCode();
                            } else  {
                                store.commit('set_token', res.data.token); // 存储token
                                store.commit('setUserInfo', this.param.username); //存储用户名
                                that.$message.success('登录成功'); //登陆成功提示
                                that.$router.push('/'); //登陆成功之后跳转到首页
                            }
                        })
                        .catch((res) => {
                            Toast.clear(); //关闭提示框
                        });
                } else {
                    this.$message.error('请输入账号和密码');
                    return false;
                }
            });
        },
        getCode() {
            getCodeImg().then((res) => {
                console.log(res);
                this.codeUrl = 'data:image/gif;base64,' + res.data.img;
                this.param.uuid = res.data.uuid;
            });
        }
    }
};
</script>

<style lang="less" scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 350px;
        margin: -190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.3);
        overflow: hidden;
        .ms-title {
            width: 100%;
            line-height: 50px;
            text-align: center;
            /* padding-left: 20px; */
            font-size: 30px;
            color: #fff;
            font-weight: bold;
            /* background-color: #409EFF; */
            background-color: #242f42;
            /* border-bottom: 1px solid #ddd; */
        }

        .ms-content {
            padding: 30px 30px;

            .login-btn {
                text-align: center;
                .el-button {
                    width: 100%;
                    height: 36px;
                    margin-bottom: 10px;
                }
            }

            .login-code {
                width: 33%;
                height: 38px;
                float: right;
                img {
                    cursor: pointer;
                    vertical-align: middle;
                }
            }
            .login-code-img {
                height: 35px;
            }
        }
    }
}
</style>